<?php
/*
	$Id: ask_a_question.php,v 2.1 2006/01/24 23.55:01 pl-xtcs Exp $

	osCommerce, Open Source E-Commerce Solutions
	http://www.oscommerce.com

	Copyright (c) 2003 osCommerce

	Released under the GNU General Public License
	Credits: Marg Davison, Lo�c Richard, FaNaTiC, C. Bouwmeester
	Version 1.1 u. Version 2.0 für XT:Commerce 3.0.4 SP1: 2005/2006 BSB Beratung+Software Bleicher
	ASK_A_QUESTION.GIF Grafikdesign (c) 2005/2005 BSB Beratung+Software Bleicher
	Anpassungen für XT:Commerce 3.0.4 SP1 inkl. Standard SUMA Funktion: 24.01.2006 Leemann Patrick xtc-services.ch

*/

// create smarty elements
$module_smarty = new Smarty;

// include needed functions
require_once (DIR_FS_INC. 'xtc_validate_email.inc.php');
include ('display_vvcodes.php');

// validate and send message
if(isset($_SESSION['customer_id'])) {
	$account_query = xtc_db_query("SELECT
										customers_firstname,
										customers_lastname,
										customers_email_address
									FROM
										customers
									WHERE
										customers_id = '" . $_SESSION['customer_id'] . "'");
	$account = xtc_db_fetch_array($account_query);
	$from_name = $account['customers_firstname'] . ' ' . $account['customers_lastname'];
	$from_email_address = $account['customers_email_address'];
}

if(isset($_POST['action']) && $_POST['action'] == 'process') {
	$error = false;

	$from_email_address = xtc_db_prepare_input($_POST['from_email_address']);
	$from_name = xtc_db_prepare_input($_POST['from_name']);
	$message = xtc_db_prepare_input($_POST['message']);
	$ask_subject = xtc_db_prepare_input($_POST['subject']);

	if (empty($from_name)) {
		$error = true;
		$messageStack->add('friend', ERROR_FROM_NAME);
	}

	if (empty($message)) {
		$error = true;
		$messageStack->add('friend', ERROR_MESSAGE);
	}
	
	if (!xtc_validate_email($from_email_address)) {
		$error = true;
		$messageStack->add('friend', ERROR_FROM_ADDRESS);
	}

	if (empty($_POST["stringCaptcha"])) {
			$error = true;
			$messageStack->add('friend', TEXT_WRONG_CODE);
	} else {
		include_once('captcha_math_check.php');
		if(!$resultCaptcha) {
			$error = true;
			$messageStack->add('friend', TEXT_WRONG_CODE);
		}
	}

	if ($error == false) {

		$module_smarty->assign('MESSAGE', $_POST['message']);
		$module_smarty->assign('TEXT_NAME', $_POST['from_name']);
		$module_smarty->assign('FROM_EMAIL_ADDRESS', $_POST['from_email_address']);
		$module_smarty->assign('PRODUCT_NAME', $product->data['products_name'].($product->data['products_model']!=''?' - Artikel-Nr.: '.$product->data['products_model']:''));
		$module_smarty->assign('PRODUCT_LINK', xtc_href_link(FILENAME_PRODUCT_INFO,'products_id='.$product->data['products_id']));
		$module_smarty->assign('PRODUCT_IMAGE', $product_info['products_image']);

		require_once (DIR_FS_INC.'cseo_get_mail_body.inc.php');
		$html_mail = nl2br($_POST['message_body']);
		$html_mail .= $module_smarty->fetch('html:askaquestion');
		$html_mail .= $signatur_html;
		$txt_mail = $_POST['message_body'];
		$txt_mail .= $module_smarty->fetch('txt:askaquestion');
		$txt_mail .= $signatur_text;
		require_once (DIR_FS_INC.'cseo_get_mail_data.inc.php');
		$mail_data = cseo_get_mail_data('askaquestion');
		$ask_subject = $_POST['subject'];

		xtc_php_mail($_POST['from_email_address'],
						$_POST['from_name'],
						$mail_data['EMAIL_ADDRESS'],
						$mail_data['EMAIL_ADDRESS_NAME'],
						$mail_data['EMAIL_FORWARD'],
						$_POST['from_email_address'],
						$_POST['from_name'],
						'',
						'',
						$ask_subject,
						$html_mail,
						$txt_mail);

		$module_smarty->assign('ALL_OK', PRODUCT_ASK_A_QUESTION_SUCCESS);
	}

}

if($messageStack->size('friend') > 0)
	$module_smarty->assign('error_message', $messageStack->output('friend'));

$eintrag1_tmp = array( 'id'   => PRODUCT_AKS_A_QUESTION_SUBJECT_1,
					 'text' => PRODUCT_AKS_A_QUESTION_SUBJECT_1 );
$eintrag2_tmp = array( 'id'   => PRODUCT_AKS_A_QUESTION_SUBJECT_2,
					 'text' => PRODUCT_AKS_A_QUESTION_SUBJECT_2 );
$eintrag3_tmp = array( 'id'   => PRODUCT_AKS_A_QUESTION_SUBJECT_3,
					 'text' => PRODUCT_AKS_A_QUESTION_SUBJECT_3 );

$subject = array( $eintrag1_tmp, $eintrag2_tmp, $eintrag3_tmp);
$subject_default = PRODUCT_AKS_A_QUESTION_SUBJECT_1; 

$module_smarty->assign('FORM_START', xtc_draw_form('email_friend', xtc_href_link(FILENAME_PRODUCT_INFO, 'products_id='.$product->data['products_id']).'#paq').xtc_draw_hidden_field('action', 'process'));
$module_smarty->assign('FROM_NAME', xtc_draw_input_field('from_name', $_POST['from_name']!=''?$_POST['from_name']:$from_name));
$module_smarty->assign('FROM_SUBJECT', xtc_draw_pull_down_menu('subject', $subject, $subject_default));
$module_smarty->assign('VVIMG', '<img src="'.$vvimg.'" alt="Captcha" />'); 
$module_smarty->assign('INPUT_CODE', xtc_draw_input_field('stringCaptcha', '', 'size="6" maxlength="6"', 'text', false));
$module_smarty->assign('CodeCaptcha', xtc_draw_hidden_field('codeCaptcha', $_SESSION["vvcode"], ''));
$module_smarty->assign('FROM_EMAIL', xtc_draw_input_field('from_email_address', $_POST['from_email_address']!=''?$_POST['from_email_address']:$from_email_address));
$module_smarty->assign('TEXTFIELD', xtc_draw_textarea_field('message', 'soft', 68, 8));
$module_smarty->assign('BUTTON_SEND', xtc_image_submit('button_continue.gif', IMAGE_BUTTON_SEND));
$module_smarty->assign('FORM_END', '</form>');

$module_smarty->assign('language', $_SESSION['language']);
$module_smarty->caching = 0;

$ask_content = $module_smarty->fetch(CURRENT_TEMPLATE.'/module/products_ask_a_question.html');

$info_smarty->assign('MODULE_products_ask_a_question', $ask_content);

#echo '<pre>';
#print_r ($_SESSION);
#echo '</pre>';

?>